﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class AddNews : System.Web.UI.UserControl
    {
        public string filePathNewsIamge;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            if (fulImageNews.HasFile)
            {
                if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                {
                    if (txtTitle.Text != "")
                    {
                        if (txtSummary.Text != "")
                        {
                            if (CKEditorNoidung.Text != "")
                            {
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathNewsIamge = "/Images/Image-News/" + fulImageNews.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-News/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                                string EncodeNoidung = Server.HtmlEncode(CKEditorNoidung.Text);
                                string values = "'" + filePathNewsIamge + "',N'" + txtTitle.Text + "',N'" + txtSummary.Text + "',N'" + EncodeNoidung + "','" + DateTime.Now.ToString() + "'";
                               
                                DAL.DCollector.DBCollector.Instance.StoredInsert("tbl_News", values);
                                Response.Redirect("/Admin/Index.aspx?panel=7&sub=10");
                            }
                            else
                            {
                                Response.Write("<script>alert('Bạn vui lòng nhập tiêu đề tin tức')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Vui lòng nhập tóm tắt nội dung')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Bạn vui lòng nhập nội dung tin tức')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng upload ảnh minh họa')</script>");
            }



        }
    }
}